<!--
  @Description:自定义导航-分类配置编辑
  @Author:zqy
  @Date:2023-08-28 13:35:14
  @Last Modified by:zqy
  @Last Modified time:2023-08-28 13:35:14
-->
<template>
  <el-dialog
    ref="refDlg"
    :title="`${!!(detailData && detailData.id) ? '编辑' : '新增'}分类配置`"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :hide-func-bar="true"
    :before-close="clearForm"
    width="500px"
    custom-class="custom-dlg"
    @close="closeDialog"
  >
    <div>
      <el-form ref="form" :model="formData" :rules="formRules" size="mini" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="分类名称" prop="tabName">
              <el-input v-model="formData.tabName" size="mini" placeholder="请输入导航名称" maxlength="30" show-word-limit />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="是否显示" prop="state">
              <el-switch v-model="formData.state" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
          <el-col v-if="Number(formData.state || 0) ===1 && isLevelOne" :span="16">
            <el-form-item
              label="显示位置"
              prop="showPlatform"
              :rules="[
                {required:Number(formData.state || 0) ===1 && isLevelOne,message:'请选择显示位置',trigger:'change'}
              ]"
            >
              <el-select v-model="formData.showPlatform" multiple clearable style="width:100%">
                <el-option value="mobile" label="移动端" />
                <el-option value="pad" label="PAD" />
                <el-option value="pc" label="PC" />
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item
              label="自定义页面:"
              prop="gotoValue"
              :rules="[
                { required:true, message: '请选择自定义页面', trigger: 'change' }
              ]"
            >
              <el-input v-model="formData.gotoName" placeholder="请选择" @focus="visibleDlg = true">
                <i slot="suffix" class="el-input__icon el-icon-search" @click.self="visibleDlg = true" />
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">确定</el-button>
    </div>
    <CustomPageList
      v-if="visibleDlg"
      :visible.sync="visibleDlg"
      :selected-id="formData.gotoValue"
      @change="dlgSelectSuccess"
    />
  </el-dialog>
</template>
<script>
import CustomPageList from '@/components/RelateCategoy/CustomPageList'
import { saveTabConfig } from '@/api/tabConf'
export default {
  name: 'CateTabConfigEdit',
  components: { CustomPageList },
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    tabDefineId: {
      // 自定义导航ID
      type: String,
      default: ''
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      submiting: false,
      formData: {
        tabName: '',
        parent: '0',
        levelType: 'FIRST', // 层级类型 一级:FIRST 二级:SECOND 三级:THIRD
        state: 1,
        showPlatform: ['mobile'], // 显示平台： mobile 移动端， pad  PAD端 ， pc PC端
        picUrl: '',
        picName: '',
        gotoType: 'CUSTOM_PAGE',
        gotoValue: '',
        gotoName: ''
      },
      formRules: {
        tabName: [
          { required: true, message: '请输入导航名称', trigger: 'blur' }
        ],
        state: [
          { required: true, message: '请选择是否显示', trigger: 'blur' }
        ]
      },
      visibleDlg: false
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    },
    isLevelOne() {
      return this.formData.levelType === 'FIRST'
    }
  },
  watch: {

  },
  created() {
    if (this.detailData) {
      const { parent, levelType } = this.detailData
      this.formData.parent = parent
      this.formData.levelType = levelType
      if (this.detailData.id) {
        this.initFormData(this.detailData)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {

    })
  },
  methods: {
    initFormData(data) {
      this.formData = {
        tabName: data.tabName || '', // 导航名称
        parent: data.parent || '0',
        levelType: data.levelType || 'FIRST',
        state: ['1', 1, '0', 0].includes(data.state) ? Number(data.state) : 1,
        showPlatform: data.showPlatform || '',
        picUrl: data.picUrl || '',
        picName: data.picName || '',
        gotoType: data.gotoType || 'CUSTOM_PAGE',
        gotoValue: data.gotoValue || '',
        gotoName: data.gotoName || ''
      }
    },
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        tabName: '',
        levelType: 'FIRST',
        state: 1,
        showPlatform: '',
        picUrl: '',
        picName: '',
        gotoType: '',
        gotoValue: '',
        gotoName: ''
      }
      this.$refs.form && this.$refs.form.clearValidate()
      done && done()
    },
    dlgSelectSuccess(data) {
      const gotoValue = data.id || ''
      const gotoName = data.name || ''
      this.$set(this.formData, 'gotoValue', gotoValue)
      this.$set(this.formData, 'gotoName', gotoName)
    },
    // 提交表单处理
    submitForm() {
      if (this.submiting) return
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          if (!this.formData.gotoValue) {
            this.$message.warning('请选择自定义页面')
            return false
          }

          const showPlatform = this.formData.showPlatform.join(',')

          const formData = {
            ...this.formData,
            ...{ showPlatform, v2TabDefineId: this.tabDefineId }
          }
          if (this.detailData && this.detailData.id) {
            formData.id = this.detailData.id
          }

          this.submiting = true
          await saveTabConfig(formData).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message.success(`${formData.id ? '编辑' : '新增'}保存成功`)
              this.$emit('success', !!formData.id)
              this.closeDialog()
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
